package com.qianying.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianying.entity.SysRolePermission;
import com.qianying.mapper.SysRolePermissionMapper;
import com.qianying.service.SysRolePermissionService;
import com.qianying.comment.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色权限关联 Service 实现类
 * 继承 ServiceImpl，实现 SysRolePermissionService 接口
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService {

    @Autowired
    SysRolePermissionMapper rolePermissionMapper;

    @Override
    public CommonResult<?> getPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRolePermission> page, SysRolePermission sysRolePermission) {
        // 可在此处添加分页查询逻辑，结合条件构造器进行复杂查询，这里先简单示例
        return CommonResult.success(page(page));
    }

    @Override
    public CommonResult<SysRolePermission> getById(Long id) {
        SysRolePermission sysRolePermission = getBaseMapper().selectById(id);
        if (sysRolePermission != null) {
            return CommonResult.success(sysRolePermission);
        }
        return CommonResult.failed("未找到对应的角色权限关联记录");
    }

    @Override
    public CommonResult<?> saveSysRolePermission(SysRolePermission sysRolePermission) {
        // 基本参数校验
        if (sysRolePermission.getRoleId() == null || sysRolePermission.getPermissionId() == null) {
            return CommonResult.failed("角色ID和权限ID不能为空");
        }
        sysRolePermission.setId(null);
        boolean saveFlag = save(sysRolePermission);
        return saveFlag ? CommonResult.success("新增成功") : CommonResult.failed("新增失败");
    }

    @Override
    public CommonResult<?> updateSysRolePermission(SysRolePermission sysRolePermission) {
        boolean updateFlag = updateById(sysRolePermission);
        return updateFlag ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }

    @Override
    public CommonResult<?> removeById(Long id) {
        if (id == null) {
            return CommonResult.success("Id不能为空");
        }
        int row = rolePermissionMapper.deleteById(id);
        if (row > 0) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult<Map<String, Boolean>> getRolePermissionMap() {
        // 1. 查询所有角色权限关联关系
        List<SysRolePermission> allRelations = rolePermissionMapper.selectList(null);
        Map<String, Boolean> rolePermissionMap=new HashMap<>();
        for (SysRolePermission allRelation : allRelations) {
            String key=allRelation.getRoleId().toString()+"_"+allRelation.getPermissionId();
            rolePermissionMap.put(key,true);
        }
        return CommonResult.success(rolePermissionMap);
    }
}